<?php
namespace app\api\controller;

use app\common\controller\Init;
use think\Controller;
use think\Db;
use think\Request;
use \app\api\model\Option;
use \app\api\model\User;
use \app\api\model\FileManage;
use \app\api\model\ShareHandler;
use think\Session;


class Batch extends Init {


	/**
	 * [index description]
	 * @Author   Aaron
	 * @DateTime 2017-07-03
	 * @return   [type]     [description]
	 */
	public function index(){
		return "";
	}
	 
	/**
	 * [List description]
	 * @Author   Aaron
	 * @DateTime 2017-07-03
	 */
	public function batchList(){
		$reqPath = input('param.folder');
        success(FileManage::batchList($reqPath,$this->UID));
	}

	public function Delete(){
	    $Post = input('param.');
		$reqPath = [];
		$dirPath = [];

		foreach ($Post as $item) {
		    if($item['type'] == 'dir') {
                $dirPath[] = $item['id'];
            } else if($item['type'] == 'file') {
                $reqPath[] = $item['id'];
            }
        }
        return FileManage::deleteItems($dirPath, $reqPath, $this->UID);
	}

	public function Move(){
		$reqPath = json_decode(file_get_contents("php://input"),true)['items'];
		$dirPath = json_decode(file_get_contents("php://input"),true)['dirs'];
		$newPath = json_decode(file_get_contents("php://input"),true)['newPath'];
		return FileManage::MoveHandler($reqPath,$dirPath,$newPath,$this->UID);
	}

	public function Rename(){
	    $reName = input('param.re_name');
	    $id = input('param.id');
	    $type = input('param.type');
	    if($type == 'folder') {
            return FileManage::folderRename($reName,$id,$this->UID);
        } else if($type == 'file') {
            return FileManage::RenameHandler($reName,$id,$this->UID);
        } else {
            error();
        }

	}

	public function Preview(){
		$reqPath = $_GET["path"];
		$fileObj = new FileManage($reqPath,$this->UID);
		$Redirect = $fileObj->PreviewHandler();
		if($Redirect[0]){
			$this->redirect($Redirect[1],302);
		}
	}
	
	public function ListPic(){
		$reqPath = $_GET["path"];
		return FileManage::listPic($reqPath,$this->UID);
	}

	public function Download(){
		$reqPath = $_GET["path"];
		$fileObj = new FileManage($reqPath,$this->UID);
		$FileHandler = $fileObj->Download();
		if($FileHandler[0]){
			$this->redirect($FileHandler[1],302);
		}
	}

	public function Share(){
		$reqPath = json_decode(file_get_contents("php://input"),true)['item'];
		$shareType = json_decode(file_get_contents("php://input"),true)['shareType'];
		ShareHandler::createShare($reqPath,$shareType,$this->userObj->getSQLData(),$this->userObj->getGroupData());
	}

	public function gerSource(){
		$reqPath = $_POST["path"];
		$fileObj = new FileManage($reqPath,$this->UID);
		$FileHandler = $fileObj->Source();
	}

	public function Content(){
		$reqPath = json_decode(file_get_contents("php://input"),true)['item'];
		$fileObj = new FileManage($reqPath,$this->UID);
		$FileHandler = $fileObj->getContent();
	}

	public function Edit(){
		$reqPath = json_decode(file_get_contents("php://input"),true)['item'];
		$fileContent = json_decode(file_get_contents("php://input"),true)['content'];
		$fileObj = new FileManage($reqPath,$this->UID);
		$FileHandler = $fileObj->saveContent($fileContent);
	}

	public function OssDownload(){
		return view('oss_download', [
			'url'  => urldecode(input("get.url")),
			'name' => urldecode(input("get.name")),
		]);
	}

	public function DocPreview(){
		$filePath = input("get.path");
		$fileObj = new FileManage($filePath,$this->UID);
		$tmpUrl = $fileObj->signTmpUrl();
		$this->redirect("http://view.officeapps.live.com/op/view.aspx?src=".urlencode($tmpUrl),302);
	}

	public function Thumb(){
		$filePath = input("get.path");
		if(input("get.isImg") != "true"){
			return "";
		}
		$fileObj = new FileManage($filePath,$this->UID);
		$Redirect = $fileObj->getThumb();
		if($Redirect[0]){
			$this->redirect($Redirect[1],302);
		}
	}

	public function GoogleDocPreview(){
		$filePath = input("get.path");
		$fileObj = new FileManage($filePath,$this->UID);
		$tmpUrl = $fileObj->signTmpUrl();
		$this->redirect("https://docs.google.com/viewer?url=".urlencode($tmpUrl),302);
	}

	/**
	 * [createFolder description]
	 * @Author   Aaron
	 * @DateTime 2017-07-03
	 * @return   [type]     [description]
	 */
	public function createFolder(){
		$reqPath = input('post.folder');
        $pdir_id = input('post.pdir_id');
		$pdir = FileManage::getFileNameById($this->UID, $pdir_id);
		$pathSplit = explode("/",ltrim($reqPath,'/'));
        $dirName = $pathSplit[count($pathSplit) -1];
        $Position = $pdir['position_absolute'] == '/' ? $pdir['position_absolute'] : $pdir['position_absolute'] . '/';
        $dirPosition = $Position;
        $folderId = FileManage::createFolder($dirName,$dirPosition,$this->UID);
		if($folderId > 0) {
            success([],200, 'ok');
        }

		error('',500);
	}
}